//
//  DocotorInfoViewController.m
//  QuickCure
//
//  Created by Ruihaha on 16/9/12.
//  Copyright © 2016年 haoge. All rights reserved.
//

#import "DocotorInfoViewController.h"
#import "TreatmentConditionController.h"
#import "DocotorDiscribeController.h"
#import "ChatViewController.h"

#import <UIImageView+WebCache.h>
#define kFilePath  [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0] stringByAppendingPathComponent:@"info.data"]


@interface DocotorInfoViewController ()<UIScrollViewDelegate>

@property(nonatomic,strong)UIView * topView;
@property(nonatomic,strong)UIView * bottomView;
@property(nonatomic,strong)UIView * segment;
@property(nonatomic,strong)UIScrollView * scrollView;


@property(nonatomic,strong)UIImageView * icon;
@property(nonatomic,strong)UILabel * nameLabel;
@property(nonatomic,strong)UILabel * professional;
@property(nonatomic,strong)UILabel * hospital;
@property(nonatomic,strong)UIButton * order;
@property(nonatomic,strong)UIButton*  flower;
@property(nonatomic,strong)UIButton * flag;


@property(nonatomic,strong)UIButton * SelectedBtn;
@property(nonatomic,strong)UIButton * FocusBtn;
@property(nonatomic,strong)UIView * indicator;
@property (nonatomic, strong) MASConstraint *centerXConstraint;

@end

@implementation DocotorInfoViewController

-(void)loadView{
    
    [super loadView];
    
        self.view.backgroundColor = [UIColor colorWithRed:0.8744 green:0.8744 blue:0.8744 alpha:1.0];
    
    if ([self respondsToSelector:@selector(edgesForExtendedLayout)]) {
        [self setEdgesForExtendedLayout:UIRectEdgeNone];
    }
}

#pragma mark ---
#pragma mark ---导航条按钮设置

-(void)focusThisDoctor:(UIButton*)btn{
    
    btn.selected = !btn.selected;
    
    NSLog(@"%d",btn.selected);
    
    [[NSUserDefaults standardUserDefaults] setBool:btn.selected forKey:self.model.doctor_name];
}

//坑点
-(instancetype)init{
    
    if (self =[super init]) {

        [self setupBaseViews];
        
    }
    
    return self;
}

#pragma mark ---
#pragma mark ---界面总体布局
-(void)setupBaseViews{

    self.topView = [[UIView alloc]init];
    self.topView.backgroundColor=[UIColor whiteColor];
    self.bottomView =[[UIView alloc]init];
    self.bottomView.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:self.topView];
    [self.view addSubview:self.bottomView];
    
    [self.topView makeConstraints:^(MASConstraintMaker *make) {
        make.top.leading.trailing.equalTo(self.view);
        make.height.equalTo(150);
        make.bottom.equalTo(self.bottomView.mas_top).offset(-10);
        
    }];
    
    [self.bottomView makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.topView.mas_bottom).offset(10);
        make.leading.trailing.equalTo(self.view);
        make.bottom.equalTo(self.view);
    }];

    [self setupBottomView];
}

#pragma mark ---
#pragma mark ---顶部view布局

-(void)setModel:(DoctorModel *)model{
    
    _model = model;
    
    
    _FocusBtn= [[UIButton alloc]initWithFrame:CGRectMake(40, 40, 40, 40)];
    [_FocusBtn setImage:[UIImage imageNamed:@"kongxin"] forState:UIControlStateNormal];
    [_FocusBtn setImage:[UIImage imageNamed:@"shixin"] forState:UIControlStateSelected];
    [_FocusBtn addTarget:self action:@selector(focusThisDoctor:) forControlEvents:UIControlEventTouchUpInside];
    //读取存档
    _FocusBtn.selected = [[NSUserDefaults standardUserDefaults] boolForKey:self.model.doctor_name];
    
    UIBarButtonItem * rightItem = [[UIBarButtonItem alloc]initWithCustomView:_FocusBtn];
    self.navigationItem.rightBarButtonItem=rightItem;

    
    //创建医生头像
    self.icon = [[UIImageView alloc]init];

    if (model.doctor_portrait!=nil) {
        
        [self.icon sd_setImageWithURL:[NSURL URLWithString:model.doctor_portrait]];
    }else{
        self.icon.image = [UIImage imageNamed:@"illness_img_person.png"];
    }
    
    self.icon.layer.cornerRadius=35;
    self.icon.clipsToBounds=YES;

    [self.topView addSubview:self.icon];
    [self.icon makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.topView.mas_top).offset(15);
        make.left.equalTo(self.topView.mas_left).offset(15);
        make.size.equalTo(CGSizeMake(70, 70));
    }];
    
    //医生姓名
    self.nameLabel = [[UILabel alloc]init];
    self.nameLabel.text = model.doctor_name;
    self.nameLabel.font=[UIFont systemFontOfSize:16];
    self.nameLabel.textColor=[UIColor blackColor];
    self.nameLabel.numberOfLines=0;
    self.nameLabel.textAlignment=NSTextAlignmentLeft;
    [self.topView addSubview:self.nameLabel];
    [self.nameLabel makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.icon.mas_right).offset(20);
        make.top.equalTo(self.topView).offset(20);
        
    }];
    
    //医生职称
    self.professional = [[UILabel alloc]init];
    if ([model.doctor_title_name isEqualToString:@"其他"]) {
        self.professional.text = @"主治医师";
    }else{
        self.professional.text = model.doctor_title_name;
        
    }
    self.professional.font=[UIFont systemFontOfSize:16];
    self.professional.textColor=[UIColor lightGrayColor];
    self.professional.numberOfLines=0;
    self.professional.textAlignment=NSTextAlignmentLeft;
    [self.topView addSubview:self.professional];
    [self.professional makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.nameLabel.mas_right).offset(10);
        make.top.equalTo(self.topView).offset(20);
        
    }];
    
    
    
    //所在医院
    self.hospital = [[UILabel alloc]init];
    self.hospital.text =model.doctor_hospital_name;
    self.hospital.font=[UIFont systemFontOfSize:16];
    self.hospital.textColor=[UIColor lightGrayColor];
    self.hospital.numberOfLines=0;
    self.hospital.textAlignment=NSTextAlignmentLeft;
    [self.topView addSubview:self.hospital];
    [self.hospital makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.icon.mas_right).offset(20);
        make.bottom.equalTo(self.icon.mas_bottom);
        
    }];
    
    
    
    
    NSMutableArray * buttonArray = [NSMutableArray arrayWithCapacity:3];
    
    
    //预约icon
    self.order = [[UIButton alloc]init];
    [buttonArray addObject:self.order];
    [self.order setImage:[UIImage imageNamed:@"yuyueliang"] forState:UIControlStateNormal];
    [self.order setTitle:[NSString stringWithFormat:@"%@",model.operation_count] forState:UIControlStateNormal];
    [self.order setTitleColor:kThemeColor forState:UIControlStateNormal];
    [self.order.titleLabel setFont:[UIFont systemFontOfSize:14]];
    [self.topView addSubview:self.order];
    
    
    //鲜花icon
    self.flower = [[UIButton alloc]init];
    [buttonArray addObject:self.flower];
    [self.flower setImage:[UIImage imageNamed:@"xianhua"] forState:UIControlStateNormal];
    [self.flower setTitle:[NSString stringWithFormat:@"%@",model.operation_count] forState:UIControlStateNormal];
    [self.flower setTitleColor:kThemeColor forState:UIControlStateNormal];
    [self.flower.titleLabel setFont:[UIFont systemFontOfSize:14]];
    [self.topView addSubview:self.flower];
    
    
    //锦旗icon
    self.flag = [[UIButton alloc]init];
    [buttonArray addObject:self.flag];
    [self.flag setImage:[UIImage imageNamed:@"jinqi"] forState:UIControlStateNormal];
    [self.flag setTitle:[NSString stringWithFormat:@"%@",model.operation_count] forState:UIControlStateNormal];
    [self.flag setTitleColor:kThemeColor forState:UIControlStateNormal];
    [self.flag.titleLabel setFont:[UIFont systemFontOfSize:14]];
    [self.topView addSubview:self.flag];
    
    
    [buttonArray mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:15 leadSpacing:20 tailSpacing:20];
    
    [buttonArray makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.hospital.mas_bottom).offset(10);
        make.bottom.equalTo(self.topView).offset(-10);
    }];
    
}

#pragma mark ---
#pragma mark ---底部view布局

-(void)setupBottomView{
    
    //创建导航
    self.segment = [[UIView alloc]init];
    [self.bottomView addSubview:self.segment];
    
    //创建滚动区域
    self.scrollView = [[UIScrollView alloc]init];
    self.scrollView.delegate = self;
    self.scrollView.scrollEnabled=NO;
    self.scrollView.pagingEnabled = YES;
    self.scrollView.contentSize=CGSizeMake(2*SCREEN_WIDTH, 0);
    self.scrollView.backgroundColor = [UIColor yellowColor];
    [self.bottomView addSubview:self.scrollView];
    
    //创建咨询按钮
    UIButton * queryBtn = [[UIButton alloc]init];
    [queryBtn setBackgroundColor:kThemeColor];
    queryBtn.layer.cornerRadius=5;
    queryBtn.clipsToBounds=YES;
    [queryBtn addTarget:self action:@selector(jumpToChatController) forControlEvents:UIControlEventTouchUpInside];
    [queryBtn setTitle:@"咨询医生" forState:UIControlStateNormal];
    queryBtn.titleLabel.textAlignment=NSTextAlignmentCenter;
    [self.bottomView addSubview:queryBtn];
    
    [self.segment makeConstraints:^(MASConstraintMaker *make) {
        make.top.leading.trailing.equalTo(self.bottomView);
        make.height.equalTo(60);
    }];
    

    [self.scrollView makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.segment.bottom);
        make.bottom.equalTo(self.bottomView).offset(-50);
        make.leading.equalTo(0);
        make.width.equalTo(SCREEN_WIDTH*2);
    }];
    
    [queryBtn makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(self.scrollView.bottom).offset(5);
        make.left.equalTo(15);
        make.right.equalTo(-15);
        make.bottom.equalTo(self.bottomView).offset(-5);
    }];
    
    [self setupSegment];
    [self setupScrollView];
    
    
}

-(void)changePage:(UIButton*)btn{
    
    if (btn.tag ==_SelectedBtn.tag) {
        return;
    }
    _SelectedBtn.selected=NO;

    if (btn.tag ==1) {
        btn.selected = YES;
        [self.scrollView setContentOffset:CGPointMake(0, 0) animated:YES];
        _centerXConstraint.equalTo(-SCREEN_WIDTH/4);
        [UIView animateWithDuration:0.3 animations:^{
         [self.view layoutIfNeeded];
     }];
       
        
    }else{
        btn.selected = YES;
        [self.scrollView setContentOffset:CGPointMake(SCREEN_WIDTH, 0) animated:YES];
        _centerXConstraint.equalTo(SCREEN_WIDTH/4);
        [UIView animateWithDuration:0.3 animations:^{
            [self.view layoutIfNeeded];
        }];
          }

    //更新indicator约束

    _SelectedBtn = btn;

}

#pragma mark ---
#pragma mark ---导航布局

-(void)setupSegment{
    
    UIButton * selection1 = [[UIButton alloc]init];
    [selection1 setTitle:@"就诊条件" forState:UIControlStateNormal];
    [selection1 setTitleColor:[UIColor lightGrayColor]  forState:UIControlStateNormal];
    [selection1 setTitleColor:kThemeColor forState:UIControlStateSelected];
    selection1.tag=1;
    selection1.selected=YES;
    self.SelectedBtn=selection1;
    
    [selection1 addTarget:self action:@selector(changePage:) forControlEvents:UIControlEventTouchUpInside];
    [self.segment addSubview:selection1];

    
    UIView * seporator = [[UIView alloc]init];
    [seporator setBackgroundColor:[UIColor colorWithRed:0.9153 green:0.9153 blue:0.9153 alpha:1.0]];
    [self.segment addSubview:seporator];


    UIButton * selection2 = [[UIButton alloc]init];
    selection2.tag=2;
    [selection2 addTarget:self action:@selector(changePage:) forControlEvents:UIControlEventTouchUpInside];
    [selection2 setTitle:@"医生简介" forState:UIControlStateNormal];
    [selection2 setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    [selection2 setTitleColor:kThemeColor forState:UIControlStateSelected];
    [self.segment addSubview:selection2];
    
    
    UIView * indicator = [[UIView alloc]init];
    self.indicator = indicator;
    [self.segment addSubview:indicator];
    indicator.backgroundColor = kThemeColor;
    
    
    [seporator makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.segment).offset(10);
        make.center.equalTo(self.segment);
        make.height.equalTo(40);
        make.width.equalTo(1);
    }];
    
    
    [selection1 makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(seporator);
        make.leading.equalTo(self.segment);
        make.trailing.equalTo(seporator.mas_leading);
        make.height.equalTo(40);
    }];
    

    [selection2 makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(seporator);
        make.leading.equalTo(selection1.mas_trailing);
        make.trailing.equalTo(self.segment);
        make.height.equalTo(40);
    }];
    


    [indicator makeConstraints:^(MASConstraintMaker *make) {
       
        make.width.equalTo(100);
        _centerXConstraint = make.centerX.equalTo(-SCREEN_WIDTH/4);
        make.bottom.equalTo(self.segment.mas_bottom);
        make.height.equalTo(2);
        
    }];
    
}

#pragma mark ---
#pragma mark ---滚动tableView布局

-(void)setupScrollView{

    TreatmentConditionController * page1 =[[TreatmentConditionController alloc]initWithStyle:UITableViewStylePlain];
    [self addChildViewController:page1];

    DocotorDiscribeController * page2 =[[DocotorDiscribeController alloc]initWithStyle:UITableViewStyleGrouped];
    [self addChildViewController:page2];
    
    UITableView * tableView1 = page1.tableView;
    UITableView * tableView2 = page2.tableView;
    
    [self.scrollView addSubview:tableView1];
    [self.scrollView addSubview:tableView2];

    
    [tableView1 makeConstraints:^(MASConstraintMaker *make) {
    
        make.top.left.equalTo(0);
        //依据self.bottom就可以. 依据self.scrollView就失败
        make.bottom.equalTo(self.bottomView).offset(-50);
        make.width.equalTo(SCREEN_WIDTH);
    
    }];
    
    
    [tableView2 makeConstraints:^(MASConstraintMaker *make) {
      
        //这样写就成功了
        make.top.equalTo(0);
        make.left.equalTo(SCREEN_WIDTH);
        make.bottom.equalTo(self.bottomView).offset(-50);
        make.width.equalTo(SCREEN_WIDTH);

        //之前这么写约束就有问题
//        make.top.equalTo(0);
//        make.bottom.equalTo(self.scrollView.mas_bottom);
//        make.width.equalTo(SCREEN_WIDTH);
    //    make.left.equalTo(self.scrollView).offset(SCREEN_WIDTH);
        
    }];

}

-(void)jumpToChatController{

    ChatViewController * chatVC = [[ChatViewController alloc]init];
    chatVC.model = self.model;
    chatVC.title=[NSString stringWithFormat:@"与%@医生的对话",_model.doctor_name];
    [self.navigationController pushViewController:chatVC animated:YES];
}
@end
